﻿using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using RestfulApiV1.Data;
using RestfulApiV1.Data.CMS;
using RestfulApiV1.Data.Entity;
using RestfulApiV1.Implement.Data;
using RestfulApiV1.Interface.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace RestfulApiV1.Utils.DataBase
{
    public class DbInitializer
    {
        public static void Seed(IServiceProvider serviceProvider)
        {
            using(var scoped = serviceProvider.CreateScope())
            {
                var db = scoped.ServiceProvider.GetService(typeof(RestfulApiDbContext)) as RestfulApiDbContext;

                db.Database.EnsureCreated();

                var hasData = db.Users.Any();

                if (!hasData)
                {
                    var userRespository = scoped.ServiceProvider.GetService(typeof(IRespository<Users>)) as EfRespository<Users>;
                    var roleRespository = scoped.ServiceProvider.GetService(typeof(IRespository<Roles>)) as EfRespository<Roles>;
                    var userRoleRespository = scoped.ServiceProvider.GetService(typeof(IRespository<UserRole>)) as EfRespository<UserRole>;
                    var articleRespository = scoped.ServiceProvider.GetService(typeof(IRespository<Articles>)) as EfRespository<Articles>;
                    var articleTypeRespository = scoped.ServiceProvider.GetService(typeof(IRespository<ArticleType>)) as EfRespository<ArticleType>;
                    var authorRespository = scoped.ServiceProvider.GetService(typeof(IRespository<Authors>)) as EfRespository<Authors>;

                    var role = new Roles
                    {
                        RoleName = "管理员",
                        Description = "如你所料，这是一个管理员角色"
                    };

                    roleRespository.Add(role);




                    

                    var SuperAdministrator = new Users
                    {
                        Username = "老蒋",
                        Password = "admin",
                    };

                    userRespository.Add(SuperAdministrator);
                    //添加用户
                    userRespository.Add(new List<Users>
                    {
                    new Users
                    {
                        Username = "琼瑶",
                        Password = "admin",
                    },

                    new Users
                    {
                        Username = "钱钟书",
                        Password = "123456",
                    },

                    new Users
                    {
                        Username = "金庸",
                        Password = "123456",
                    },
                    new Users
                    {
                        Username = "刘慈欣",
                        Password = "123456",
                    },
                    new Users
                    {
                        Username = "天下霸唱",
                        Password = "123456",
                    }
                });
                    
                    //添加作者
                    //authorRespository.Add(new List<Authors>
                    //{
                    //     new Authors{
                    //        AuthorName = "琼瑶",
                    //        AuthorProvince = "四川成都人",
                    //        AuthorSex = "女"
                    //     },

                    //     new Authors{
                    //        AuthorName = "钱钟书",
                    //        AuthorProvince = "江苏无锡人",
                    //        AuthorSex = "男"
                    //     },

                    //     new Authors{
                    //        AuthorName = "金庸",
                    //        AuthorProvince = "浙江海宁人",
                    //        AuthorSex = "男"
                    //     },

                    //     new Authors{
                    //        AuthorName = "刘慈欣",
                    //        AuthorProvince = "河南信阳人",
                    //        AuthorSex = "男"
                    //     },

                    //     new Authors{
                    //        AuthorName = "天下霸唱",
                    //        AuthorProvince = "天津人",
                    //        AuthorSex = "男"
                    //     }
                    //});
                  

                    //添加文学类型
                    articleTypeRespository.Add(new List<ArticleType>
                    {
                        new ArticleType{
                            ArticleTypeName="长篇言情"
                        },
                        new ArticleType{   
                            ArticleTypeName="长篇讽刺"
                        },
                        new ArticleType{
                            ArticleTypeName="长篇武侠"
                        },
                        new ArticleType{
                            ArticleTypeName="长篇科幻"
                        },
                        new ArticleType{
                            ArticleTypeName="长篇奇幻"
                        }
                    });

                    //添加文学作品
                    articleRespository.Add(new List<Articles>
                    {
                        new Articles{
                            ArticlesTypesId=5,
                            UsersId=2,
                            ArticleFragment=" 人类什么东西都可以分享，只有爱情，怎么能够分享呢？--陆依萍",
                            ArticleName="《情深深雨蒙蒙》"
                        },
                        new Articles{
                            ArticlesTypesId=4,
                            UsersId=3,
                            ArticleFragment="睡眠这东西脾气怪得很，不要它，它偏会来；请它，哄它，千方百计勾引它，它便躲得连影子都不见。--方鸿渐",
                            ArticleName="《围城》"
                        },
                        new Articles{
                            ArticlesTypesId=3,
                            UsersId=4,
                            ArticleFragment="杨过抬起头来,只见一只白玉般的纤手掀开帷幕,走进一个少女来.那少女披着一袭轻纱般的白衣,犹似身在烟中雾里,看来约莫十六七岁年纪,除了一头黑发之外,全身雪白,面容秀美绝俗,只是肌肤间少了一层血色,显得苍白异常。",
                            ArticleName="《神雕侠侣》"
                        },
                        new Articles{
                            ArticlesTypesId=2,
                            UsersId=5,
                            ArticleFragment="当然不害怕，她知道太阳明天还会升起来的。——罗辑 ",
                            ArticleName="《三体》"
                        },
                        new Articles{
                            ArticlesTypesId=1,
                            UsersId=6,
                            ArticleFragment="发丘印，摸金符，搬山卸岭寻龙诀；人点蜡，鬼吹灯，勘舆倒斗觅星峰；水银癍，养明器，龙楼宝殿去无数；窨沉棺，青铜椁，八字不硬莫近前。 ----胡八一",
                            ArticleName="《鬼吹灯》"
                        },
                    });

                    //添加用户身份
                    userRoleRespository.Add(new List<UserRole>
                    {
                        new UserRole
                        {
                            UsersId=SuperAdministrator.Id,
                            RolesId=role.Id
                        }
                       
                    });


                }

            }
        }
    }
}
